<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="支付名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入支付名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="payConfigList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="名称" align="center" prop="name"/>
          <el-table-column label="商户号" align="center" prop="mercharntId"/>
          <el-table-column label="应用ID" align="center" prop="appId"/>
          <el-table-column label="代付回调地址" align="center" prop="payNotifyUrl"/>
          <el-table-column label="代收回调地址" align="center" prop="virtualNotifyUrl"/>
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="虚拟卡类型" align="center" prop="vaType">
            <template slot-scope="scope">
             <span v-for="item in typeList" v-if="scope.row.vaType == item.code">{{item.name}}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" v-if="scope.row.userId !== 1">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="支付名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入支付名称" maxlength="30"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="支付公司" prop="payCompany">
              <el-radio-group v-model="form.payCompany">
                <el-radio
                  v-for="item in payCompanyList"
                  :key="item.code"
                  :label="item.code"
                >{{item.name}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="商户号" prop="mercharntId">
              <el-input v-model="form.mercharntId" placeholder="请输入商户号"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="应用ID" prop="appId">
              <el-input v-model="form.appId" placeholder="请输入应用ID"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="代付回调地址" prop="payNotifyUrl">
              <el-input v-model="form.payNotifyUrl" placeholder="请输入代付回调地址"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="代收回调地址" prop="virtualNotifyUrl">
              <el-input v-model="form.virtualNotifyUrl" placeholder="请输入代收回调地址"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="秘钥" prop="publicKey">
              <el-input type="textarea" :rows="3" v-model="form.publicKey"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="虚拟卡类型" prop="vaType">
              <el-radio-group v-model="form.vaType">
                <el-radio
                  v-for="item in typeList"
                  :key="item.code"
                  :label="item.code"
                >{{item.name}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {listPayConfig, getPayConfig, addPayConfig, updatePayConfig, changeStatus, delPayConfig,getPayCompanyList} from "@/api/app/pay/config";

  export default {
    name: "PayConfig",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户表格数据
        payConfigList: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 表单参数
        form: {},
        payCompanyList: null,
        typeList: [
          {
            code: '1',
            name: '虚拟卡'
          },
          {
            code: '2',
            name: '虚拟账户'
          }
        ],

        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined
        },

        // 表单校验
        rules: {
          name: [
            {required: true, message: "名称不能为空", trigger: "blur"},
          ],
          mercharntId: [
            {required: true, message: "商户号不能为空", trigger: "blur"},
          ],
          appId: [
            {required: true, message: "应用ID不能为空", trigger: "blur"},
          ],
          payCompany: [
            {required: true, message: "支付公司不能为空", trigger: "blur"},
          ],
          publicKey: [
            {required: true, message: "密钥不能为空", trigger: "blur"},
          ]
        }
      };
    },
    watch: {},
    created() {
      this.getList();
      this.getPayList();
    },
    methods: {
      /** 查询用户列表 */
      getList() {
        this.loading = true;
        listPayConfig(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.payConfigList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },

      getPayList() {
        getPayCompanyList().then(response => {
            this.payCompanyList = response.data;
            console.log(this.payCompanyList)
          }
        );
      },

      // 用户状态修改
      handleStatusChange(row) {
        let text = row.status === "0" ? "启用" : "停用";
        this.$modal.confirm('确认要"' + text + '""' + row.name + '"吗？').then(function () {
          return changeStatus(row.id, row.status);
        }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          mercharntId: undefined,
          appId: undefined,
          payCompany: undefined,
          virtualNotifyUrl: undefined,
          payNotifyUrl: undefined,
          publicKey: undefined,
          vaType: '1'
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },

      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加支付通道";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getPayConfig(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改支付通道";
        });
      },


      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updatePayConfig(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPayConfig(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除编号为"' + row.id + '"的支付通道？').then(function () {
          return delPayConfig(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/user/export', {
          ...this.queryParams
        }, `user_${new Date().getTime()}.xlsx`)
      },
    }
  };
</script>
